package com.ohs.ohsoa.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ohs.ohsoa.system.dto.RoleRouterDto;
import com.ohs.ohsoa.system.dto.RouterDto;
import com.ohs.ohsoa.system.entity.RoleRouter;
import com.ohs.ohsoa.system.entity.Router;
import com.ohs.ohsoa.system.mapper.RouterMapper;
import com.ohs.ohsoa.system.vo.RouterVo;
import com.ohs.ohsoa.utils.IsNull;
import com.ohs.ohsoa.utils.bean.requestBean.PageParam;
import com.ohs.ohsoa.utils.exception.BizException;
import org.springframework.beans.BeanUtils;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 页面serviceImpl
 *
 * @author ccj
 * @since  2021年10月11日
 */
@Service
public class RouterServiceImpl extends ServiceImpl<RouterMapper, Router> implements RouterService {


    @Resource
    private RouterMapper routerMapper;

    @Resource
    private RoleRouterService roleRouterService;

    @Resource
    private MessageSource messageSource;

    @Override
    public List<Router> routerSelectList(PageParam<Router> pageParam) {
        Router router = pageParam.getSearchbean();
        // 分页
        PageHelper.startPage(pageParam.getIndexPage(), pageParam.getPageSize());
        return routerMapper.routerSelectList(router);
    }

    @Override
    public Router routerSelect(Router router) {
        return this.getById(router.getRouterGuid());
    }

    @Override
    public Boolean routerInsert(Router router) {
        this.repeatRouter(router);
        router.setDeleteFlag("1");
        return this.save(router);
    }

    /**判断routerName是否重复
     *
     * @param router router信息
     */
    private void repeatRouter(Router router) {
        QueryWrapper<Router> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("router_name",router.getRouterName()).ne(IsNull.isNotNull(router.getRouterGuid()),"router_guid",router.getRouterGuid());
        if(this.count(queryWrapper) > 0){
            throw new BizException("E00017", messageSource.getMessage("E00017",null, null)) ;
        }
    }

    @Override
    public Boolean routeDelete(RouterDto routerDto) {
        return routerMapper.routeDelete(routerDto.getRouterGuids());
    }

    @Override
    public Boolean routerUpdate(Router router) {
        this.repeatRouter(router);
        return this.saveOrUpdate(router);
    }

    @Override
    public List<RoleRouter> RoleRouterList(RoleRouter roleRouter) {
        QueryWrapper<RoleRouter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(IsNull.isNotNull(roleRouter.getRouterGuid()), "router_guid", roleRouter.getRouterGuid())
                .eq(IsNull.isNotNull(roleRouter.getRoleGuid()), "role_guid", roleRouter.getRoleGuid());
        return roleRouterService.list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean RoleRouterUpdate(RoleRouterDto roleRouterDto) {
        //父级页面id
        List<String> pRouterGuids = this.list().stream().filter(item -> item.getPguid().isEmpty()).map(Router::getRouterGuid).collect(Collectors.toList());
        roleRouterDto.getRouterGuids().removeAll(pRouterGuids);
        QueryWrapper<RoleRouter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(IsNull.isNotNull(roleRouterDto.getRoleGuid()), "role_guid", roleRouterDto.getRoleGuid());
        roleRouterService.remove(queryWrapper);
        for (String routerGuid : roleRouterDto.getRouterGuids()) {
            RoleRouter roleRouter = new RoleRouter();
            roleRouter.setRoleGuid(roleRouterDto.getRoleGuid());
            roleRouter.setRouterGuid(routerGuid);
            roleRouterService.save(roleRouter);
        }
        return true;
    }

    @Override
    public List<RouterVo> routerAllList(String groupEnterpriseGuid) {
        QueryWrapper<Router> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("group_enterprise_guid", groupEnterpriseGuid).eq("delete_flag", "1");
        List<Router> routers = this.list(queryWrapper);
        List<Router> pRouters = routers.stream().filter(item -> item.getPguid().isEmpty()).collect(Collectors.toList());
        List<RouterVo> routerVos = new ArrayList<>();
        for (Router router : pRouters) {
            RouterVo routerVo = new RouterVo();
            BeanUtils.copyProperties(router, routerVo);
            routerVo.setChildren(routers.stream().filter(item -> item.getPguid().equals(router.getRouterGuid())).collect(Collectors.toList()));
            routerVos.add(routerVo);
        }
        return routerVos;
    }

    @Override
    public List<Router> parentRouterList(String groupEnterpriseGuid) {
        QueryWrapper<Router> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("group_enterprise_guid", groupEnterpriseGuid).eq("delete_flag", "1").orderByAsc("sort_flag");
        List<Router> routers = this.list(queryWrapper);
        return routers.stream().filter(item -> item.getPguid().isEmpty()).collect(Collectors.toList());
    }

    @Override
    public List<Router> childrenRouterList(String routerGuid) {
        QueryWrapper<Router> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_flag", "1").eq("pguid", routerGuid).orderByAsc("sort_flag");
        return this.list(queryWrapper);
    }


}
